<%
  # encoding: utf-8
  #
  # Redmine plugin for Document Management System "Features"
  #
  # Copyright © 2011-20 Karel Pičman <karel.picman@kontron.com>
  #
  # This program is free software; you can redistribute it and/or
  # modify it under the terms of the GNU General Public License
  # as published by the Free Software Foundation; either version 2
  # of the License, or (at your option) any later version.
  #
  # This program is distributed in the hope that it will be useful,
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  # GNU General Public License for more details.
  #
  # You should have received a copy of the GNU General Public License
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% parent = @folder ? @folder : @project %>
<% all_children_sorted(parent, @pos, @idnt).each do |obj, position| %>
    <% classes = "dmsf_tree idnt-#{@idnt} hascontextmenu" %>
    <% if obj.is_a?(DmsfFolder) && ((obj.dmsf_folders.visible.any?) || (obj.dmsf_files.visible.any?) || (obj.dmsf_links.visible.any?)) %>
        <% classes += ' idnt dmsf_collapsed dmsf-not-loaded' %>
        <% id = "id='#{obj.id}span'".html_safe %>
        <% onclick = "onclick=\"dmsfToggle('#{obj.id}','#{obj.id}span','#{escape_javascript(expand_folder_dmsf_path)}')\"" %>
    <% else %>
        <% classes += ' dmsf_child' %>
        <% onclick = '' %>
    <% end %>
    <% parent = obj.dmsf_folder %>
    <% while parent %>
        <% classes += " #{parent.id}" %>
        <% parent = parent.dmsf_folder %>
    <% end %>

    <% if obj.is_a? DmsfFolder %>
        <% classes << ' dmsf_system' if obj.system %>
        <% classes << ' dmsf_system_closed' if obj.issue && obj.issue.closed? %>
        <tr <%= id %> class="dir <%= classes %>">
          <%= render(:partial => 'dir',
                     :locals => {
                         :project => @project,
                         :subfolder => obj,
                         :link => nil,
                         :id => obj.id,
                         :name => 'folder',
                         :title => obj.title,
                         :onclick => onclick,
                         :position => position}) %>
        </tr>
    <% elsif obj.is_a?(DmsfLink) && (obj.target_type == 'DmsfFolder') %>
        <% unless obj.target_project %>
            <% Rails.logger.error "Error: dmsf_link id #{obj.id} has no target!" %>
            <% next %>
        <% end %>
        <tr <%= id %> class="dmsf_gray <%= classes %>">
          <%= render(:partial => 'dir',
                     :locals => {
                         :project => obj.target_project,
                         :subfolder => obj.target_folder,
                         :link => obj,
                         :id => obj.id,
                         :name => 'folder-link',
                         :title => obj.name,
                         :onclick => onclick,
                         :position => position}) %>
        </tr>
    <% elsif obj.is_a?(DmsfFile) %>
        <% unless obj.last_revision %>
            <% Rails.logger.error "Error: dmsf_file id #{obj.id} has no revision!" %>
            <% next %>
        <% end %>
        <tr <%= id %> class="file <%= classes %>">
          <%= render(:partial => 'file', :locals => {
              :project => @project,
              :file => obj,
              :link => nil,
              :id => obj.id,
              :name => 'file',
              :title => obj.title,
              :onclick => onclick,
              :position => position}) %>
        </tr>
    <% elsif obj.is_a?(DmsfLink) && (obj.target_type == 'DmsfFile') %>
        <% unless obj.target_file && obj.target_file.last_revision %>
            <% Rails.logger.error "Error: dmsf_file id #{obj.target_id} has no revision!" %>
            <% next %>
        <% end %>
        <tr <%= id %> class="dmsf_gray <%= classes %>">
          <%= render(:partial => 'file', :locals => {
              :project => obj.target_project,
              :file => obj.target_file,
              :link => obj,
              :id => obj.id,
              :name => 'file-link',
              :title => obj.name,
              :onclick => onclick,
              :position => position}) %>
        </tr>
    <% elsif obj.is_a?(DmsfLink) && (obj.target_type == 'DmsfUrl') %>
        <tr <%= id %> class="dmsf_gray <%= classes %>">
          <%= render(:partial => 'url', :locals => {
              :project => obj.target_project,
              :file => obj.target_file,
              :link => obj,
              :id => obj.id,
              :name => 'url-link',
              :title => obj.name,
              :onclick => onclick,
              :position => position}) %>
        </tr>
    <% end %>
<% end %>
